import { Component, HostListener, OnDestroy, OnInit } from "@angular/core";
import { CommService } from "../comm-service";
import { LocalStorageService } from "../local-storage-service";
import { Router } from "@angular/router";
import { MyhttpService } from "../myhttp.service";

declare var swal: any;
declare var $: any;

@Component({
  selector: "topH",
  template: `
    <div class="page-header navbar">
      <div class="page-logo">
        <a href="javascript:void(0)">
          <img [src]="logourl" class="logo-default" alt="logo"/>
        </a>
      </div>
      <!-- end logo  -->
      <!-- begin responsive menu toggler-->
      <a href="javascript:void(0);" class="menu-toggler responsive-toggler" data-toggle="collapse"
         data-target=".navbar-collapse">
      </a>
      <!-- end responsive menu toggler-->
      <!-- bgein  top navigation  menu -->
      <div class="top-menu">
        <ul class="nav navbar-nav pull-right">
          <!--<li class="dropdown dropdown-extended dropdown-notification" id="header_task_bar">-->
          <!--<a href="javascript:void(0);" title="待办任务数量" class="dropdown-toggle lc" data-toggle="dropdown"-->
          <!--data-hover="dropdown"-->
          <!--data-close-others="true" (click)="goto()">-->
          <!--<span class="glyphicon glyphicon-tasks font-red" *ngIf="hasnewtask"></span>-->
          <!--<span class="badge badge-success" *ngIf="hasnewtask"> {{taskcount}} </span>-->
          <!--<span class="glyphicon glyphicon-tasks" *ngIf="!hasnewtask"></span>-->
          <!--<span class="badge" *ngIf="!hasnewtask"> {{taskcount}} </span>-->
          <!--</a>-->
          <!--</li>-->
          <!--<li class="dropdown dropdown-extended dropdown-notification" id="header_ggao_bar">-->
          <!--<a href="javascript:void(0);" title="待收公文数量" class="dropdown-toggle lc" data-toggle="dropdown"-->
          <!--data-hover="dropdown"-->
          <!--data-close-others="true" (click)="goto()">-->
          <!--<span class="glyphicon glyphicon-envelope font-red" *ngIf="hasnewgg"></span>-->
          <!--<span class="badge badge-success" *ngIf="hasnewgg"> {{myggaocount}} </span>-->
          <!--<span class="glyphicon glyphicon-envelope" *ngIf="!hasnewgg"></span>-->
          <!--<span class="badge" *ngIf="!hasnewgg"> {{myggaocount}} </span>-->
          <!--</a>-->
          <!--</li>-->
          <!-- DOC: Apply "dropdown-dark" class after below "dropdown-extended" to change the dropdown styte -->
          <li class="dropdown dropdown-extended dropdown-notification">
            <a href="javascript:void(0)" class="dropdown-toggle lnamec" dropdown-menu-hover data-toggle="dropdown"
               data-close-others="true">
              <label class="lc" title="用户名"> <span class="glyphicon glyphicon-user"></span>{{loginName}}</label>
            </a>
          </li>
          <!--<li class="dropdown dropdown-extended dropdown-inbox">-->
          <!--<a class="dropdown-toggle lnamec" dropdown-menu-hover-->
          <!--data-toggle="dropdown" data-close-others="true" (click)="editePWD()">-->
          <!--<label class="lc" title="修改密码"><i class="fa fa-pencil-square-o font-blue"></i> 修改密码</label>-->
          <!--</a>-->
          <!--</li>-->
          <!-- DOC: Apply "dropdown-dark" class after below "dropdown-extended" to change the dropdown styte -->
          <!--<li class="dropdown dropdown-extended dropdown-inbox">-->
          <!--<a class="dropdown-toggle lnamec" dropdown-menu-hover-->
          <!--data-toggle="dropdown" data-close-others="true" (click)="helpMsg()">-->
          <!--<label class="lc" title="帮助"><i class="fa fa-info-circle"></i> 帮助</label>-->
          <!--</a>-->
          <!--</li>-->
          <li class="dropdown dropdown-extended dropdown-inbox">
            <a class="dropdown-toggle lnamec" dropdown-menu-hover
               data-toggle="dropdown" data-close-others="true" (click)="exitSYS()">
              <label class="lc" title="返回"><i class="fa fa-reply font-blue"></i> 返回</label>
            </a>
          </li>
          <li>
            <button class="btn btn-sm hidden-xs" style="background-color:#2b3643" data-toggle="collapse"
                    data-target="#content2" (click)="fixedContentH('btn')">
              <i class="fa fa-level-up" style="color:#79869a;font-size:16px;margin-top:15px" aria-hidden="true"></i>
            </button>
          </li>
          <li>
            <button class="btn btn-sm hidden-lg hidden-md hidden-sm" style="background-color:#2b3643"
                    data-toggle="collapse" data-target="#defaultNavbar1">
              <i class="fa fa-level-up" style="color:#505b6b; font-size:16px; margin-top:15px"
                 aria-hidden="true"></i>
            </button>
          </li>
        </ul>
      </div>
      <!-- end  top navigation  menu -->
    </div>
    <!--<ng-template #editepwd></ng-template>-->
  `,
  styles: [
    '.lnamec{background: none}',
    '.lc{color: #D6E9C6;cursor: pointer;}',
  ]
})
export class TopHComponent implements OnInit, OnDestroy {
  loginName: string;
  logourl: string = "";
  // taskcount: number = 0;
  // myggaocount: number = 0;
  // hasnewtask: boolean = false;
  // hasnewgg: boolean = false;
  $iframedom: any;
  $myMask: any;
  sysBase: string;

  /**
   * 右上角的 隐藏/显示  topmenu 的页面高度处理
   * @param {string} t
   */
  fixedContentH(t: string): void {
    const topmenu = this.localstorage.get("TMselect");
    let h: number = 0;
    if ($("#content2").hasClass('in')) {
      h = this.commservice.getFitHeight("in", t);
    } else {
      h = this.commservice.getFitHeight("out", t);
    }
    $(".pageroom").css("height", h); // 上下结构
    $(".page-content").css("height", h); // 上左右结构
  }

  /**
   * 帮助文档
   */
  helpMsg(): void {
    const obj = this.commservice.getViewPort();
    this.$myMask = $("#myMask");
    this.$myMask.css({
      width: obj.width + "px",
      height: obj.height + "px"
    });
    const ifscr = this.commservice.HOST + "/assets/helpmsg/index.html";
    if (!this.commservice.isframeExist(this.$iframedom)) {
      this.$myMask.show();
      this.$iframedom = this.commservice.initIframePz("helpmsg", 1200, 900, ifscr, null);
    }
  }


  goto(): void {

  }


  getTJ(): void {
    // this.itservice.requstUrl("security_client/auth/getStatus", null).subscribe(resp => {
    //   const retflag = resp.result;
    //   if (retflag == "success") {
    //     const rows = resp.rows;
    //     const obj = rows[0];
    //     if (this.taskcount == 0) {
    //       this.taskcount = Number(obj.dbrwsl);
    //     } else {
    //       //任务
    //       if (obj.dbrwsl == this.taskcount) {
    //         //没有新的任务
    //         this.hasnewtask = false;
    //       } else {
    //         //有新的任务
    //         this.hasnewtask = true;
    //       }
    //       this.taskcount = Number(obj.dbrwsl);
    //     }
    //
    //     //公告
    //     if (this.myggaocount == 0) {
    //       this.myggaocount = Number(obj.djsgwsl);
    //     } else {
    //       if (obj.djsgwsl == this.myggaocount) {
    //         //没有新的公告
    //         this.hasnewgg = false;
    //       } else {
    //         //有新的公告
    //         this.hasnewgg = true;
    //       }
    //       this.myggaocount = Number(obj.djsgwsl);
    //     }
    //   }
    // });
  }

  getUserInfo(): void {
    this.commservice.getUserinfo((info) => {
      this.loginName = info.name ? decodeURIComponent(info.name) : "";
    });
  }

  /**
   * 退出业务模块，返回 控制模块
   */
  exitSYS(): void {
    if (this.sysBase == "61") {
      this.router.navigate(['/ctrsx']);
    } else {
      this.router.navigate(['/ctrzj']);
    }
  }

  @HostListener("window:resize", ["$event"])
  onResize(event) {
    this.fixedContentH("screen");
  }


  /**
   *   公共子组件
   *  不接受父组件参数，
   *  不回传给父组件任何参数
   *
   * @param {CommService} commservice
   * @param {LocalStorageService} localstorage
   * @param {Router} router
   */
  constructor(private commservice: CommService,
    private localstorage: LocalStorageService,
    private router: Router,
    private itservice: MyhttpService) {
  }


  ngOnInit() {
    this.commservice.getSysmsg2((resp) => {
      if (resp) {
        this.logourl = "assets/img/" + resp.syslogonames + ".png";
        this.sysBase = resp.sysBase;
      }
    });
    this.getUserInfo();
    //this.getTJ();
  }

  ngOnDestroy() {

  }
}
